package com.ydzt.fanka.server.utils;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;

public class IOUtil {
	public static void writeFile(byte bytes[],String fileName){
		FileOutputStream fos=null;   
        try{   
            fos=new FileOutputStream(fileName,true);   
            fos.write(bytes);   
            fos.flush();     
        }     
        catch(Exception e){   
            e.printStackTrace();   
            //Logger.getInstance().logPrint("writeObject", "дbyte�������:"+e.getMessage());   
        }   
        finally{   
            try{   
                fos.close();   
            }   
            catch(IOException iex){}   
        } 
	}
	
	public static boolean writeTxtFile (String newStr,String filenameTemp) throws IOException { 
		
		// �ȶ�ȡԭ���ļ����ݣ�Ȼ�����д����� 
		boolean flag = false; 
		String filein = newStr + "\r\n"; 
		String temp = ""; 
		
		FileInputStream fis = null; 
		InputStreamReader isr = null; 
		BufferedReader br = null; 
		FileOutputStream fos = null; 
		PrintWriter pw = null; 
		try {
			// �ļ�·�� 
			File file = new File(filenameTemp); 
			// ���ļ����������� 
			fis = new FileInputStream(file); 
			isr = new InputStreamReader(fis); 
			br = new BufferedReader(isr); 
			StringBuffer buf = new StringBuffer(); 
			// ������ļ�ԭ�е����� 
			for (@SuppressWarnings("unused")
			int j = 1; (temp = br.readLine()) != null; j++) {
				buf = buf.append(temp);
				// System.getProperty("line.separator") 
				// ������֮��ķָ��� �൱�ڡ�\n�� 
				buf = buf.append(System.getProperty("line.separator")); 
			}
			buf.append(filein); 
			fos = new FileOutputStream(file);
			pw = new PrintWriter(fos);
			pw.write(buf.toString().toCharArray());
			pw.flush();
			flag = true;
		} 
		catch (IOException e1) {
			throw e1;
		} finally {
			if (pw != null) {
				pw.close();
			}
			if (fos != null) {
				fos.close();
			}
			if (br != null) {
				br.close();
			}
			if (isr != null) {
				isr.close();
			}
			if (fis != null) {
				fis.close();
			}
		}
		return flag; 
	} 
	
	/*
	 * 读取buffer文件进
	 * */
    public static byte[] toByteArrayTraditional(String filename) throws IOException{  
          
        File f = new File(filename);  
        if(!f.exists()){  
            throw new FileNotFoundException(filename);  
        }  
  
        ByteArrayOutputStream bos = new ByteArrayOutputStream((int)f.length());  
        BufferedInputStream in = null;  
        try{  
            in = new BufferedInputStream(new FileInputStream(f));  
            int buf_size = 8192;  
            byte[] buffer = new byte[buf_size];  
            int len = 0;  
            while(-1 != (len = in.read(buffer,0,buf_size))){  
                bos.write(buffer,0,len);  
            }  
            return bos.toByteArray();  
        }catch (IOException e) {  
            e.printStackTrace();  
            throw e;  
        }finally{  
            try{  
                in.close();  
            }catch (IOException e) {  
                e.printStackTrace();  
            }  
            bos.close();  
            in.close();
        }  
    }  
    
	/** 
     * Mapped File  way 
     * MappedByteBuffer �����ڴ�����ļ�ʱ���������� 
     * @param filename 
     * @return 
     * @throws IOException 
     */  
    @SuppressWarnings("resource")
	public static byte[] toByteArray(String filename)throws IOException{  
          
        FileChannel fc = null;  
        try{  
            fc = new RandomAccessFile(filename,"r").getChannel();  
            MappedByteBuffer byteBuffer = fc.map(MapMode.READ_ONLY, 0, fc.size()).load();   
            byte[] result = new byte[(int)fc.size()];  
            if (byteBuffer.remaining() > 0) {  
//              System.out.println("remain");  
                byteBuffer.get(result, 0, byteBuffer.remaining()); 
                byteBuffer.clear();
            }  
            return result;  
        }catch (IOException e) {  
            e.printStackTrace();  
            throw e;  
        }finally{  
            try{
                fc.close();
            }catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
}